//
//  ViewController.m
//  TestDmallWareList
//
//  Created by ren on 2/5/17.
//  Copyright © 2017 ren. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>

@interface ViewController ()
@property (nonatomic, strong) UITextView    *textView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setButtons];
    [self setTextView];
}

#pragma mark - add views
- (void)setButtons
{
    for (int i = 0; i < 20; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, 40 * i, 60, 40)];
        [btn setBackgroundColor:[UIColor redColor]];
        [btn setTitle:[NSString stringWithFormat:@"按钮%d",i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

- (void)setTextView
{
    [self.textView setFrame:CGRectMake(60, 0, [UIScreen mainScreen].bounds.size.width - 60, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:self.textView];
}

- (UITextView *)textView
{
    if (_textView == nil) {
        _textView = [UITextView new];
    }
    return _textView;
}

#pragma mark button action
- (void)btnClick:(UIButton *)button
{
    [self requestData];
}

- (void)requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    [manager.requestSerializer setValue:@"5AE222B7-4F09-4560-B43B-AE3DBC5C4E51" forHTTPHeaderField:@"idfa"];
    [manager.requestSerializer setValue:@"3.2.0" forHTTPHeaderField:@"apiVersion"];
    [manager.requestSerializer setValue:@"3.2.0" forHTTPHeaderField:@"version"];
    [manager.requestSerializer setValue:@"IOS" forHTTPHeaderField:@"platform"];
    [manager.requestSerializer setValue:@"APPSTORE" forHTTPHeaderField:@"channelId"];
    [manager.requestSerializer setValue:@"iPhone Simulator" forHTTPHeaderField:@"device"];
    [manager.requestSerializer setValue:@"10.2" forHTTPHeaderField:@"sysVersion"];
    [manager.requestSerializer setValue:@"com.dmall.dmallmainapp" forHTTPHeaderField:@"appName"];
    [manager.requestSerializer setValue:@"667*375" forHTTPHeaderField:@"screen"];
    [manager.requestSerializer setValue:@"b24833e662e8e8b0ba8b898b9e5f1fa156fefb33" forHTTPHeaderField:@"uuid"];
    [manager.requestSerializer setValue:@"ac" forHTTPHeaderField:@"xyz"];
    
    
    [manager.requestSerializer setValue:@"appapi_b51257a043c206679f482b7952791612" forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"230" forHTTPHeaderField:@"storeId"];
    [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"venderId"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%d",1] forHTTPHeaderField:@"smartLoading"];//智能加载开关 0-关闭, 1-打开
    [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"networkType"];//网络类型：1-wifi, 2-2G, 3-3G/4G, 4-其它
    [manager.requestSerializer setValue:@"1486303274098" forHTTPHeaderField:@"currentTime"];
    
    [manager.requestSerializer setValue:@"37.785834" forHTTPHeaderField:@"lat"];
    [manager.requestSerializer setValue:@"-122.406417" forHTTPHeaderField:@"lng"];
    [manager.requestSerializer setValue:nil forHTTPHeaderField:@"gatewayCache"];
    [manager.requestSerializer setValue:@"1-230-1;2-230-1;" forHTTPHeaderField:@"storeGroup"];
    [manager.requestSerializer setValue:@"H4sIAAAAAAAAA01RS67UMBC8CrJmSSL/48yOBes3YthHjt2ZWCR25Nhv9IQ4CWdgz4bbwD2wMzzxdu6uqu7q8le0hJvzg7PojAy1I2AwTa9533BuodFaq0ZRobQiveGjRu+RdXuKzqSiKNVpNxHAD3dn04zOrBOlp7dteIa4u+ALi7W0xYWad4iPTVxhTiQvvTIKIA0mrGv2Lr38G7oGC0t5u8scPLy7ujUvOoVYMDNr7w/ww+Vy/fz06WMVmJB9ilX+++ePP99/1XX52DVSrhgDKSkoUCMetRpVr8YexEQmTYScYBoZq1M8pHuIX4b0skGRksNgiPBwTVm94nR3k0PnFDMUMwdAtAQQxHJLJgq4U9YICT2ftOiIEVBUya2wJ71u6Ey4kgwzhjEV5H+AM7jbXEKVsivNcq1L2VYXrGs7JRSrcT2Dt68hVnenJfjbK7EhlLYcS066CoX9zR8Q3NIjWO3zpE3KEWKNcNsWeJBr2k9X9O0vOxz/4BMCAAA=" forHTTPHeaderField:@"bigdata"];
    
    //    NSError *error;
    
    NSString *url = @"https://appapi.dmall.com:443/app/search/wareSearch";
    
    NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys:
                             @"10255",@"categoryId",
                             @"0",@"sortRule",
                             @"0",@"sort",
                             @"1",@"pageNum",
                             @"1",@"businessCode",
                             @"20",@"pageSize",
                             @"1",@"from",
                             @"230",@"erpStoreId",
                             @"1",@"venderId",
                             @"2",@"categoryLevel",
                             @"0",@"sortKey",
                             @"0",@"thirdCategoryId",
                             @"1",@"categoryType",
                             @"0",@"globalSelection",
                             nil];
    
    [[manager POST:url parameters:mapData progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@", uploadProgress.description);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.textView setText:[NSString stringWithFormat:@"%@",responseObject]];
            [self.textView setTextColor:[UIColor blackColor]];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.textView setText:[NSString stringWithFormat:@"%@",error.userInfo.description]];
            [self.textView setTextColor:[UIColor redColor]];
        });
    }] resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
